package cn.edu.ctbu.datajpa01.service.impl;

import cn.edu.ctbu.datajpa01.domain.Achievement;
import cn.edu.ctbu.datajpa01.domain.Schedule;
import cn.edu.ctbu.datajpa01.domain.Student;
import cn.edu.ctbu.datajpa01.repository.AchievementRepository;
import cn.edu.ctbu.datajpa01.repository.StudentRepository;
import cn.edu.ctbu.datajpa01.service.AchievementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author losen
 * @version 1.0
 * @DateTime 2023/1/10 16:51
 */

@Transactional
@Service
public class AchievementServiceImpl implements AchievementService {
    @Autowired
    AchievementRepository achievementRepository;

    @Autowired
    StudentRepository studentRepository;

    public List<Achievement> findAll(){
        return achievementRepository.findAll();
    }

    @Override
    public Page<Achievement> findAll(Pageable pageable) {
        return achievementRepository.findAll(pageable);
    }

    @Override
    public Page<Achievement> findAll(Example<Achievement> studentExample, Pageable pageable) {
        return achievementRepository.findAll(studentExample,pageable);
    }


    public void delete(Achievement achievement){
        achievementRepository.delete(achievement);
    }
    public void delete(Integer id){
        Achievement achievement=new Achievement();
        achievement.setId(id);

        achievementRepository.delete(achievement);
    }

    public Achievement getById(Integer id){

        Achievement achievement=achievementRepository.findById(id).orElse(null);
        return achievement;
    }


    public Achievement insert(Achievement achievement){
        Student byName = studentRepository.getByName(achievement.getName());
        achievement.setSid(byName.getId());

        return achievementRepository.save(achievement);

    }

    public Achievement update(Achievement achievement){
        return achievementRepository.save(achievement);
    }

    @Override
    public void deleteByName(String name) {
        achievementRepository.removeAchievementsByName(name);
    }
}
